<?php

use \Phalcon\Loader as LoaderManager;

//初始化核心配置
define('CONSOLE_PATH',dirname(__DIR__).DIRECTORY_SEPARATOR);
define('BASE_PATH',dirname(CONSOLE_PATH).DIRECTORY_SEPARATOR);

//获得所需配置
$_loaderManager     = new LoaderManager();

//注册控制台
$_loaderManager->registerNamespaces(['console'=>CONSOLE_PATH],true)->register();

//读取公共配置
$_itemConfigArray = \console\Config::loadBase('item',true);

//注册自定义命名空间
$_loaderManager->registerNamespaces(\console\Config::loadBase('config')->namespace->toArray(),true)->register();

//获取核心参数
$_paramService = \console\http\Request::get('_service');
$_paramVersion = \console\http\Request::get('_version','v1');

//如果访问首页
if(empty($_paramService))
    exit(\console\http\Response::json());

//区分判断
if(!array_key_exists($_paramService,$_itemConfigArray))
    exit(\console\http\Response::json(101));

//项目配置
$_serviceConfig = $_itemConfigArray[$_paramService]?:[];
if(empty($_serviceConfig))
    exit(\console\http\Response::json(102));

//定义项目
define('ITEM_NAME',$_paramService);
define('ITEM_PATH',$_serviceConfig['path']);

//注册项目命名空间
$_loaderManager->registerNamespaces([$_paramService=>$_serviceConfig['path']],true)->registerNamespaces(\console\Config::loadItem('config')->namespace->toArray(),true)->register();

class App
{
    static function run($RootPath = '')
    {
        define('ROOT_PATH',$RootPath);
        global $_serviceConfig;
        echo call_user_func('\console\bootstrap\\'.ucfirst($_serviceConfig['boot'])."::run");
    }
}